package com.example.zg.androidnewview.behaviors;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.Snackbar;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

/**
 * 创建日期:2019/1/10 on 11:11
 * 描述：
 * 作者: zhougan
 */
//其中有一个泛型，它的作用是指定要使用这个Behavior的View的类型，
// 可以是Button、TextView等等。如果希望所有的View都可以使用则指定泛型为View即可
public class TextViewBehavior2 extends CoordinatorLayout.Behavior<View> {
    private static final String TAG = "TextViewBehavior2";
    public TextViewBehavior2(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    //确定使用Behavior的View要依赖的View的类
    @Override
    public boolean layoutDependsOn(CoordinatorLayout parent, View child, View dependency) {
        Log.i(TAG, "layoutDependsOn: ");



        return super.layoutDependsOn(parent,child,dependency);

    }

    private float deltaY=0;
    //当被依赖的View状态改变时回调
    @Override
    public boolean onDependentViewChanged(CoordinatorLayout parent, View child, View dependency) {

        if (deltaY == 0) {
            deltaY = dependency.getY() - child.getHeight();
        }

        float dy = dependency.getY() - child.getHeight();
        dy = dy < 0 ? 0 : dy;
        float y = -(dy / deltaY) * child.getHeight();
        child.setTranslationY(y);





        return true;
    }

    //嵌套滑动开始（ACTION_DOWN），确定Behavior是否要监听此次事件
    @Override
    public boolean onStartNestedScroll(@NonNull CoordinatorLayout coordinatorLayout, @NonNull View child, @NonNull View directTargetChild, @NonNull View target, int axes, int type) {
//        return super.onStartNestedScroll(coordinatorLayout, child, directTargetChild, target, axes, type);
        Log.i(TAG, "onStartNestedScroll: ");
        return true;
    }
    float translationY=0;
    //嵌套滑动进行中，要监听的子 View的滑动事件已经被消费
    @Override
    public void onNestedScroll(@NonNull CoordinatorLayout coordinatorLayout, @NonNull View child, @NonNull View target, int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed, int type) {
      /*  Log.i(TAG, "onNestedScroll: ");
        Log.i(TAG, "onNestedScroll: child="+child);
        Log.i(TAG, "onNestedScroll: target="+target);
        Log.i(TAG, "onNestedScroll: dxConsumed="+dxConsumed);
        Log.i(TAG, "onNestedScroll: dxUnconsumed="+dxUnconsumed);
        Log.i(TAG, "onNestedScroll: dyUnconsumed="+dyUnconsumed);
        Log.i(TAG, "onNestedScroll: type="+type);*/
        Log.i(TAG, "onNestedScroll: dyConsumed="+dyConsumed);


       /* translationY+=dyConsumed;
        if(translationY>child.getHeight()){
            translationY=child.getHeight();
        }else if(translationY<0){
            translationY=0;
        }
        child.setTranslationY(-translationY);
        target.setTranslationY(child.getHeight()-translationY);
        Log.i(TAG, "onNestedScroll: getTranslationY"+target.getY());*/


        super.onNestedScroll(coordinatorLayout, child, target, dxConsumed, dyConsumed, dxUnconsumed, dyUnconsumed, type);
    }

    //当被依赖的View移除时回调
    @Override
    public void onDependentViewRemoved(CoordinatorLayout parent, View child, View dependency) {
        super.onDependentViewRemoved(parent, child, dependency);
    }

    //测量使用Behavior的View尺寸
    @Override
    public boolean onMeasureChild(CoordinatorLayout parent, View child, int parentWidthMeasureSpec, int widthUsed, int parentHeightMeasureSpec, int heightUsed) {
        return super.onMeasureChild(parent, child, parentWidthMeasureSpec, widthUsed, parentHeightMeasureSpec, heightUsed);
    }

    //确定使用Behavior的View位置
    @Override
    public boolean onLayoutChild(CoordinatorLayout parent, View child, int layoutDirection) {


        return super.onLayoutChild(parent, child, layoutDirection);
    }



    @Override
    public void onNestedScrollAccepted(@NonNull CoordinatorLayout coordinatorLayout, @NonNull View child, @NonNull View directTargetChild, @NonNull View target, int axes, int type) {
        super.onNestedScrollAccepted(coordinatorLayout, child, directTargetChild, target, axes, type);
    }

    //嵌套滑动结束（ACTION_UP或ACTION_CANCEL）
    @Override
    public void onStopNestedScroll(@NonNull CoordinatorLayout coordinatorLayout, @NonNull View child, @NonNull View target, int type) {
        super.onStopNestedScroll(coordinatorLayout, child, target, type);
    }



    //嵌套滑动进行中，要监听的子 View将要滑动，滑动事件即将被消费（但最终被谁消费，可以通过代码控制）
    @Override
    public void onNestedPreScroll(@NonNull CoordinatorLayout coordinatorLayout, @NonNull View child, @NonNull View target, int dx, int dy, @NonNull int[] consumed, int type) {
        super.onNestedPreScroll(coordinatorLayout, child, target, dx, dy, consumed, type);
    }

    //要监听的子 View在快速滑动中
    @Override
    public boolean onNestedFling(@NonNull CoordinatorLayout coordinatorLayout, @NonNull View child, @NonNull View target, float velocityX, float velocityY, boolean consumed) {
        return super.onNestedFling(coordinatorLayout, child, target, velocityX, velocityY, consumed);
    }

    //要监听的子View即将快速滑动
    @Override
    public boolean onNestedPreFling(@NonNull CoordinatorLayout coordinatorLayout, @NonNull View child, @NonNull View target, float velocityX, float velocityY) {
        return super.onNestedPreFling(coordinatorLayout, child, target, velocityX, velocityY);
    }


}
